package com.hj.security.ui;

import java.util.ArrayList;
import java.util.List;

import com.hj.security.R;
import com.hj.security.dao.AppLockDao;
import com.hj.security.domain.AppInfo;
import com.hj.security.engine.AppInfoProvider;

import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.animation.Animation;
import android.view.animation.TranslateAnimation;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

public class AppLockActivity extends Activity {

	private ListView lv_app_lock;
	private AppLockAdapter adapter;
	private List<AppInfo> list;
	private AppInfoProvider provider;
	private AppLockDao dao;
	private List<String> lockApps;

	private Handler handler = new Handler() {
		@Override
		public void handleMessage(Message msg) {
			super.handleMessage(msg);
			adapter = new AppLockAdapter();
			lv_app_lock.setAdapter(adapter);
		}
	};

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.app_lock);

		lv_app_lock = (ListView) findViewById(R.id.lv_app_lock);
		provider = new AppInfoProvider(this);
		dao = new AppLockDao(this);
		lockApps = dao.getAllPackageName();
		initappInfos();
		lv_app_lock.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				//添加动画效果，动画结束后，不把锁的图片改变
				TranslateAnimation translateAnimation = new TranslateAnimation(  
		                Animation.RELATIVE_TO_SELF, 0.0f,   
		                Animation.RELATIVE_TO_SELF, 0.5f,   
		                Animation.RELATIVE_TO_SELF, 0.0f,   
		                Animation.RELATIVE_TO_SELF, 0.0f);
				translateAnimation.setDuration(300);
				view.setAnimation(translateAnimation);
				AppInfo appInfo = list.get(position);
				ImageView iv_lock = (ImageView) view.findViewById(R.id.iv_app_lock);
				String packageName = appInfo.getPackageName();
				if(dao.find(packageName)){
					dao.delete(packageName);
					lockApps.remove(packageName);
					iv_lock.setImageResource(R.drawable.unlock);
				}else{
					dao.add(packageName);
					lockApps.add(packageName);
					iv_lock.setImageResource(R.drawable.lock);
				}
			}
		});
	}

	private void initappInfos() {
		new Thread() {
			@Override
			public void run() {
				super.run();
				list = provider.getAllApps();
				handler.sendEmptyMessage(0);
			}
		}.start();
	}

	private class AppLockAdapter extends BaseAdapter {

		@Override
		public int getCount() {
			// TODO Auto-generated method stub
			return list.size();
		}

		@Override
		public Object getItem(int position) {
			// TODO Auto-generated method stub
			return list.get(position);
		}

		@Override
		public long getItemId(int position) {
			// TODO Auto-generated method stub
			return position;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			AppManagerViews appManagerViews;
			AppInfo info = list.get(position);
			if(convertView==null){
				View view = View.inflate(AppLockActivity.this, R.layout.app_lock_item, null);
				appManagerViews = new AppManagerViews();
				appManagerViews.iv_app_lock_icon = (ImageView) view.findViewById(R.id.iv_app_lock_icon);
				appManagerViews.iv_app_lock = (ImageView) view.findViewById(R.id.iv_app_lock);
				appManagerViews.tv_app_lock_name = (TextView) view.findViewById(R.id.tv_app_lock_name);
				appManagerViews.iv_app_lock_icon.setImageDrawable(info.getIcon());
				appManagerViews.tv_app_lock_name.setText(info.getAppName());
				if(lockApps.contains(info.getPackageName())){
					appManagerViews.iv_app_lock.setImageResource(R.drawable.lock);
					//lockApps.add(info.getPackageName());
				}else{
					appManagerViews.iv_app_lock.setImageResource(R.drawable.unlock);
				}
				view.setTag(appManagerViews);
				return view;
			}else{
				appManagerViews = (AppManagerViews) convertView.getTag();
				appManagerViews.iv_app_lock_icon.setImageDrawable(info.getIcon());
				appManagerViews.tv_app_lock_name.setText(info.getAppName());
				if(lockApps.contains(info.getPackageName())){
					appManagerViews.iv_app_lock.setImageResource(R.drawable.lock);
				}else{
					appManagerViews.iv_app_lock.setImageResource(R.drawable.unlock);
				}
				return convertView;
			}
			
		}
	}

	// 用来优化listview的类
	private class AppManagerViews {
		ImageView iv_app_lock_icon;
		ImageView iv_app_lock;
		TextView tv_app_lock_name;
	}
}
